﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models.Services;
using MvcApplication2.Models;
using MvcApplication2.Models.DTO;
using System.Web.Security;

namespace MvcApplication2.Controllers
{
    public class AccountController : Controller
    {
        //public static NguoiDung NguoiDung;

        [HttpPost]
        public  ActionResult LogOn(MasterModel model)
        {
            var data = NguoiDungService.SignIn(model.TenDangNhap??string.Empty, model.MatKhau??string.Empty, model.NhoMatKhau);
            Session.Add("NguoiDung",data.Item2);
            string errorDangNhap = data.Item1;
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            return RedirectToAction("Index","Home", new{error=errorDangNhap});
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            Session.Remove("NguoiDung");
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Register()
        {
            var model = new RegisterModel();
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            return View(model);
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                model.Error = NguoiDungService.Register(model.NguoiDung);
            }
            model.LoaiSanPhamList = MasterController.LayDanhSachLoaiSanPham();
            //model.TongDiemThuong = MasterController.LayTongDiemThuong(User.Identity.Name);
            return View(model);
        }
    }
}
